package com.bigdata.hive;

import com.bigdata.auth.JaasFactory;
import com.bigdata.auth.SourceType;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;

import java.io.IOException;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;

/**
 * @author xin.ding
 * @date 2020/10/16 14:14
 */
public class HiveKerberosTest {
    private static String JDBC_DRIVER = "org.apache.hive.jdbc.HiveDriver";
    private static String CONNECTION_URL = "jdbc:hive2://tdh03:10000/;principal=hive/tdh03@HADOOP.COM";
    private static String KRB5_PATH = "D:\\code\\library\\util\\utils-all\\utils-bigdata\\src\\main\\resources\\krb5.conf";
    private static String USER = "hive/tdh03@HADOOP.COM";
    private static String KEYTAB_PATH = "D:\\code\\library\\util\\utils-all\\utils-bigdata\\src\\main\\resources\\hive.keytab";


    public static void main(String[] args) throws Exception {

        Connection connection = getConnection();
        List<String> strings = showDatabases(connection);
        System.out.println("show database!!"+strings);

    }

//    /**
//     * 构建hive Kerberos 配置信息
//     *
//     * @param krb5Path   krb5.conf路径
//     * @param user       keytab principal 名称
//     * @param keytabPath keytab文件路径
//     */
//    public static void buildHiveKerberosProp(String krb5Path, String user, String keytabPath) {
//        try {
//            System.setProperty("java.security.krb5.conf", krb5Path);
//            Configuration configuration = new Configuration();
//            configuration.set("hadoop.security.authentication", "Kerberos");
//            UserGroupInformation.setConfiguration(configuration);
//            UserGroupInformation.loginUserFromKeytab(user, keytabPath);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * 构建HiveKerberos 认证环境
     *
     * @param krb5Path  krb5.conf 路径
     * @param principal kerberos principal
     * @param keytab    keytab文件路径
     */
    public static void buildHiveKerberosProp(String krb5Path, String principal, String keytab) {
        System.setProperty("java.security.krb5.conf", krb5Path);
        Configuration conf = new Configuration();
        conf.set("hadoop.security.authentication", "Kerberos");
        String jaasPath = JaasFactory.buildJaasFile(SourceType.HIVE, principal, keytab);

        System.setProperty("java.security.auth.login.config", jaasPath);

//        UserGroupInformation.setConfiguration(conf);
//        try {
//            UserGroupInformation.loginUserFromKeytab(principal, keytabPath);
//        } catch (Exception e) {
//            logger.error("Failed to create hive Kerberos environment.{}", e.getMessage());
//            throw new AuthException("Hive Kerberos authentication failed");
//        }
    }

    /**
     * 获取Hive Jdbc连接
     * @return Connection
     */
    public static Connection getConnection() {
        Connection connection = null;
        try {
            Class.forName(JDBC_DRIVER);
            buildHiveKerberosProp(KRB5_PATH, USER, KEYTAB_PATH);
            connection = DriverManager.getConnection(CONNECTION_URL);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static List<String> showDatabases(Connection connection) {
        Statement stmt = null;
        ResultSet res = null;
        List<String> tables = new LinkedList<>();
        try {
            stmt = connection.createStatement();
            res = stmt.executeQuery("SHOW DATABASES");
            while (res.next()) {
                tables.add(res.getString(1));
            }
        } catch (SQLException e) {
            throw new HiveDBException(e);
        }
        return tables;
    }
}
